package cn.edu.ujn.booksshop.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.Payment;

import cn.edu.ujn.booksshop.service.IPaymentService;
import cn.edu.ujn.util.Page;


@Controller
public class PaymentController {

	@Autowired
	private IPaymentService paymentService;

	@GetMapping("payment/list.action")
	public String list(Model model, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer rows) {
		Page<Payment> selectAll = this.paymentService.selectAll(page, rows);
		model.addAttribute("page", selectAll);
		return "payment";
	}

	@RequestMapping("payment/create.action")
	@ResponseBody
	public String create(Payment payment) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf.format(date);
		payment.setPaymentdate(nowTime);
		payment.setIsdeleted(false);
		int insert = this.paymentService.insert(payment);
		if(insert>0) {
			return "OK";
		}else {
			return "FAIL";
		}
	}

	@RequestMapping("payment/delete.action")
	@ResponseBody
	public String delete(Integer id) {
		int delete = this.paymentService.deleteByPrimaryKey(id);
		if(delete>0) {
			return "OK";
		}else {
			return "FAIL";
		}
	}
	
//	@PostMapping("payment/update.action")
//	public String update(@RequestParam (defaultValue = "0")  Integer paymentId,@RequestParam(defaultValue ="0")  String editedPaymentMethod) {
//		Payment payment = new Payment();
//		payment.setPaymentid(paymentId);
//		payment.setPaymentmethod(editedPaymentMethod);
//		int update = this.paymentService.updateByPrimaryKeySelective(payment);
//		if(update>0) {
//			return "OK";
//		}else {
//			return "FAIL";
//		}
//	}
	
	@RequestMapping("payment/update.action")
	@ResponseBody
	public String update(Payment payment) {
	int update = this.paymentService.updateByPrimaryKeySelective(payment);
	if(update>0) {
		return "OK";
	}else {
		return "FAIL";
	}
}
	
	@RequestMapping("/payment/getPaymentById.action")
	@ResponseBody
	public Payment getPaymentById(Integer id) {
	    Payment payment = paymentService.getPaymentById(id);
	    return payment;
	}
}
